<?php
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
require('../includes/init.php');
class Syn_Template
{

	//判断模板是否生成 并且在基础数据上体现
	public function preview()
	{
	
		$syn_tabls = 
		array(
		'ws_j'=>array( 1 => array('tem_table'=>'Table_66','tem_id'=>'Field_20658','web_type'=>5) ),
		'ws_d'=>array( 1 => array('tem_table'=>'Table_62','tem_id'=>'Field_20573','web_type'=>6) ),
		'any'=>array( 1 => array('tem_table'=>'Table_55','tem_id'=>'Field_20486','web_type'=>3) ),
		'app'=>array( 1 => array('tem_table'=>'Table_51','tem_id'=>'Field_20428','web_type'=>4) ),
		'aim'=>array( 1 => array('tem_table'=>'Table_59','tem_id'=>'Field_20531','web_type'=>2) ),
		'ws'=>array( 1 => array('tem_table'=>'Table_13','tem_id'=>'Field_19965','web_type'=>1),
					 2 => array('tem_table'=>'Table_31','tem_id'=>'Field_20160','web_type'=>1), ),
		'is'=>array( 1 => array('tem_table'=>'Table_71','tem_id'=>'Field_20681','web_type'=>8) ),
		'sp'=>array( 1 => array('tem_table'=>'Table_47','tem_id'=>'Field_20382','web_type'=>7) ),
		);
		
		$type = trim($_GET["type"]);
		if(!empty($syn_tabls[$type]))
		{
			
			foreach ($syn_tabls[$type] AS $key => $values) {
				$this->update_article($values['tem_table'], $values['tem_id'], $values['web_type']);
			}
			
			echo  '<script type="text/javascript"> 
			alert("同步预览成功！");
			window.location.href="http://cms.wondershare.com/wondershare/article.php?action=index&w_type='.$syn_tabls[$type][1]['web_type'].'";
			</script>';
			exit;
		}
		if($type == 'all')
		{
			foreach ($syn_tabls AS $key => $value)
			{
				$this->update_article($value['tem_table'],$value['tem_id'],$value['web_type']);
			}
			echo  '<script type="text/javascript"> 
			alert("同步全部网站预览成功！");
			window.location.href="http://cms.wondershare.com/wondershare/article.php?action=index&w_type='.$syn_tabls['web_type'].'";
			</script>';
			exit;
		}
	}
	
	//替换is文章的<div class="focus">
	public function replace_is()
	{
		$sql = "SELECT ar_content,ar_id FROM  topic_article  WHERE   web_type =8  
		AND   `ar_content` LIKE  '%<div class=\"focus\">%'  ";
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
		
			$ar_content =  str_replace('<div class="focus">','',($row['ar_content']));
			$ar_content = addslashes(substr($ar_content,0,strrpos($ar_content,'</div>')));
			$sql  = "UPDATE topic_article SET ar_content = '{$ar_content}' WHERE ar_id = ".$row['ar_id'];
			$GLOBALS['db']->query($sql);
		}
	}
	
	public function update_article($tem_table,$tem_id,$web_type)
	{
	
		$sql = 'SELECT '.$tem_table.'.url,topic_article.ar_id FROM '.$tem_table.' ,
		topic_article WHERE '.$tem_table.'.'.$tem_id.' = topic_article.ar_id AND 
		topic_article.is_delete = 0 AND topic_article.web_type = '.$web_type;
		$result = $GLOBALS['db']->query($sql);
		while($row = $GLOBALS['db']->fetch_array($result))
		{
			$path_file = '/var/www/website/cms.wondershare.com/httpdocs/vhosts'.$row['url'];
			if(file_exists($path_file))
			{
				$template_url = 'http://check.wondershare.com'.$row['url'];
				$sql = "UPDATE topic_article SET template_url = '{$template_url}' , pub_status = 1 WHERE ar_id = ".$row['ar_id'];
				$GLOBALS['db']->query($sql);
			}
		}
	}
	
	public function is_exist_file()
	{
		$web_path = '/var/www/website/cms.wondershare.com/httpdocs/vhosts';
		$path_file = $web_path.$_GET['tem_url'];
		if($_GET['tem_tables'])
		{
			if(empty($_GET['tem_id']))//如果是新增
			{
				if(file_exists($path_file))//判断文件是否存在
				{
					echo  1;
				}else
				{
				echo  0;
				}
			}else //如果是修改
			{
				$sql = 'SELECT url FROM '.$_GET['tem_tables'].' WHERE D_ID = '.$_GET['tem_id'];
				$url = $GLOBALS['db']->getOne($sql);
				if($url != $_GET['tem_url'])
				{
					if(file_exists($path_file))//判断文件是否存在
					{
					    echo  1;
					}else
					{
					     unlink($web_path.$url);
					     echo  2;
					}
				}else
				{
					echo  3;
				}
			}
		}
	}

}

$action    = $_REQUEST['action']; 
$syn_template  = new Syn_Template;
if(method_exists($syn_template, $action)) 
{
	$syn_template->$action();
}else{
	$syn_template->index();
}



?>